import subprocess
import requests
import re
import os
from urllib3.exceptions import InsecureRequestWarning
import datetime

today = datetime.date.today()  # 今天
yester = today - datetime.timedelta(days=1)
# 禁用安全请求警告

Platform = "有书阁"


def youshuge():
    try:
        requests.urllib3.disable_warnings(InsecureRequestWarning)
        r = requests.session()
        rr = r.get(url="https://novel.youshuge.com/login", verify=False, timeout=15)
        html = rr.text.replace('\r', '').replace('\n', '').replace('\t', '')
        capt = re.findall('<img src="https://novel.youshuge.com/captcha/(.*?)"', html)
        capturl = "https://novel.youshuge.com/captcha/" + capt[0]
        cc = r.get(url=capturl, verify=False, timeout=15)
        with open("capt.jpg", "wb") as f:
            f.write(cc.content)
            # 关闭文件流对象
            f.close()
        subprocess.call('capt.jpg', shell=True)
        captcha = input("请输入验证码：")
        data = {
            "user_name": "",
            "password": "",
            "captcha": str(captcha)
        }
        os.remove("capt.jpg")
        aa = r.post(url="https://novel.youshuge.com/login", data=data, verify=False, timeout=15)
        html = aa.text.replace('\r', '').replace('\t', '').replace('\n', '')
        Amount = re.findall('<tr>\s+<td>\d+</td>\s+<td>(.*?)</td>', html)[0]
        Amount_share = round(float(Amount) * 0.9, 2)
        date_time = str(yester)
        return date_time, Amount_share, Platform
    except Exception:
        print("获取 有书阁 数据失败")
        return '', 'None', Platform
    except ConnectionError:
        print("获取 有书阁 数据 超时")
        return '', 'None', Platform